//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    array: ['威士忌', '品牌', '酒厂', '装瓶商'],
    index: 0,
    userInfo: [],
    shou: true,
    shous:true,
    imgdix:"",
    dis:1,
    tk:'',
    avatarUrls:'',
    phoneNumber:""
  },
  onLoad: function () {
    var tk = wx.getStorageSync('tk')
    
    let that=this
that.setData({
  tk:tk,
  avatarUrls:wx.getStorageSync('avatarurl')
})
    console.log(this.data.avatarUrls)
    if (tk == "") {
      // this.setData({
      //   shou: false
      // })
    } 
   


  },
  keep: function () {
    let that = this
    that.setData({

      userInfo: app.globalData.userInfo
    });
    // app.request({
    //   url: '/login/user?openid=' + wx.getStorageSync('oprnid') + '&nickname=' + wx.getStorageSync('name') + '&avatar=' + wx.getStorageSync('avatarurl'), //仅为示例，并非真实的接口地址
    //   header: {
    //     'content-type': 'application/json' // 默认值
    //   },
    //   method: 'post',
    //   success: function (res) {

    //     //从数据库获取用户信息
    //     console.log(res)
    //     wx.setStorageSync('tk', "123");
    //     console.log("插入小程序登录用户信息成功！");
    //     //授权成功后，跳转进入小程序首页

    //     that.setData({
    //       hasUserInfo: true,
    //       shou: true

    //     });
     
    //     // that.ismsign();

    //   }
    // });
    app.checkSession()
      .then(status => {
        if (status) {
          // 必须是在用户已经授权的情况下调用
          wx.getUserInfo({
            success: function (res) {

              wx.setStorage({
                key: 'name',
                data: res.userInfo.nickName,
              })
              wx.setStorage({
                key: 'avatarurl',
                data: res.userInfo.avatarUrl,
              })
              that.setData({
                avatarUrls:  res.userInfo.avatarUrl
              })
              // var userInfo = res.userInfo
              // var nickName = userInfo.nickName
              // var avatarUrl = userInfo.avatarUrl
              // var gender = userInfo.gender //性别 0：未知、1：男、2：女
              // var province = userInfo.province
              // var city = userInfo.city
              // var country = userInfo.country
            }
          })
        }
      })
  },
  otw() {
  
    if (wx.getStorageSync('tk') == "123" && wx.getStorageSync('phone') == "123") {
      wx.navigateTo({
        url: '../otw/otw',
      })
    } else if (wx.getStorageSync('tk') != "123"){
      this.setData({
        shou: false
      })
    }
     else if (wx.getStorageSync('phone') != "123"){
      this.setData({
        shous: false
      })
    }
  
  },
  bindGetUserInfo(e) {
    let that=this
    if (e.detail.userInfo) {
      //用户按了允许授权按钮
      let that = this;
      console.log(e)
      let usercodes = wx.getStorageSync('usercodes')
      that.setData({
        avatarUrls:e.detail.userInfo.avatarUrl
      })
      console.log(this.data.avatarUrls)
      // console.log('/********用户信息******/',usercodes)
      app.globalData.userInfo = e.detail.userInfo;
      // console.log(e.detail.userInfo)
      //插入登录的用户的相关信息到数据库
      app.request({
        url: '/login/user', //仅为示例，并非真实的接口地址
       data:{
         'nickname':e.detail.userInfo.nickName,
         'avatar':e.detail.userInfo.avatarUrl
       },
       method:'post',
        success: function (res) {
          //从数据库获取用户信息
          console.log(res)
          wx.setStorageSync('tk', "123");
          wx.setStorageSync('avatarurl', e.detail.userInfo.avatarUrl);
          console.log("插入小程序登录用户信息成功！");
          //授权成功后，跳转进入小程序首页
          
          that.setData({
            hasUserInfo: true,
            shou: true,
            shous:false,
            avatarUrls:wx.getStorageSync('avatarurl')

          });
          that.keep()
          // that.ismsign();

        }
      });
    }
  },
  //获取手机号
  getPhoneNumber: function (e) {
    var that = this;
    console.log(e)
    console.log(e.detail.errMsg == "getPhoneNumber:ok");
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      app.request({
        url: '/login/get-phone-number',
      
        data: {
          'encryptedData': e.detail.encryptedData,
          'iv': e.detail.iv,
          
        },
        method:'post',
        success: function (res) { 
          if(res.errCode=='00'){
             that.setData({
            phoneNumber:res.data.tel
          })
            app.request({
              url: '/login/update-user?phone=' + that.data.phoneNumber,
              success:function(res){
                wx.setStorageSync('phone', '123')
                 that.setData({
                   shous:true
                 })
              }
            })
          }
         
      
        }
      })
    }
  },
  //点击收藏
  onCollectTap: function (event) {
    if (wx.getStorageSync('tk') == "123" && wx.getStorageSync('phone') == "123") {
    wx.navigateTo({
      url: '../collect/collect',
    })
    } else if (wx.getStorageSync('tk') != "123") {
      this.setData({
        shou: false
      })
    }
    else if (wx.getStorageSync('phone') != "123") {
      this.setData({
        shous: false
      })
    }
  },
  //点击想喝
  onWantTap: function (event) {
    
    // wx.navigateTo({
    //   url: '../want/want',
    // })
    if (wx.getStorageSync('tk') == "123" && wx.getStorageSync('phone') == "123"){
      wx.navigateTo({
        url: '../myotw/myotw',
      })
    }
    else if (wx.getStorageSync('tk') != "123") {
      this.setData({
        shou: false
      })
    }
    else if (wx.getStorageSync('phone') != "123") {
      this.setData({
        shous: false
      })
    }
   
  },
  //点击喝过
  onDrinkedTap: function (event) {
    if (wx.getStorageSync('tk') == "123" && wx.getStorageSync('phone') == "123"){
      wx.navigateTo({
        url: '../drinked/drinked',
      })
    }
    else if (wx.getStorageSync('tk') != "123") {
      this.setData({
        shou: false
      })
    }
    else if (wx.getStorageSync('phone') != "123") {
      this.setData({
        shous: false
      })
    }
    
  },
  //点击品饮
  onNotesTap: function () {
    if (wx.getStorageSync('tk') == "123" && wx.getStorageSync('phone') == "123") {
    wx.navigateTo({
      url: '../note/noteListSelf/noteListSelf',
    })
    }
    else if (wx.getStorageSync('tk') != "123") {
      this.setData({
        shou: false
      })
    }
    else if (wx.getStorageSync('phone') != "123") {
      this.setData({
        shous: false
      })
    }
  },
  //点击wb
  onBaseTap: function (event) {
    wx.navigateTo({
      url: '../h5/wb/wb',
    })
  },
  //点击wf
  onWfTap: function (event) {
    wx.navigateTo({
      url: '../h5/wf/wf',
    })
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  squx(){
    this.setData({
      shou: true
    })
  },
  squxs() {
    this.setData({
      shous: true
    })
  },
  //点击品牌
  async goFiltess(e) {
    if (wx.getStorageSync('tk') == "123" && wx.getStorageSync('phone') == "123"){
      const status = await app.getAuth()
      if (!status) return
      console.log(e.currentTarget.dataset.typeid)
      let typeId = e.currentTarget.dataset.typeid
      if (e && e.currentTarget.dataset.typeid) {
        wx.navigateTo({
          url: '/pages/filterLists/index?typeId=' + typeId
        })
      }
    }
    else if (wx.getStorageSync('tk') != "123") {
      this.setData({
        shou: false
      })
    }
    else if (wx.getStorageSync('phone') != "123") {
      this.setData({
        shous: false
      })
    }
    
  },
  async goFilter(e) {
    if (wx.getStorageSync('tk') == "123" && wx.getStorageSync('phone') == "123") {
      const status = await app.getAuth()
      if (!status) return
      console.log(e.currentTarget.dataset.typeid)
      let typeId = e.currentTarget.dataset.typeid
      if (e && e.currentTarget.dataset.typeid) {
        wx.navigateTo({
          url: '/pages/filterList/index?typeId=' + typeId
        })
        return
      }
      wx.showToast({
        title: '参数不完整',
        icon: 'none'
      })
    } else if (wx.getStorageSync('tk') != "123") {
      this.setData({
        shou: false
      })
    }
    else if (wx.getStorageSync('phone') != "123") {
      this.setData({
        shous: false
      })
    }
  },
  async goSearch(e) {
    if (wx.getStorageSync('tk') == "123" && wx.getStorageSync('phone') == "123") {
      if (this.data.index == 0) {
        wx.navigateTo({
          url: '/pages/filterLists/index?typeId=' + 0
        })
      }
      else {
        wx.navigateTo({
          url: '/pages/filterList/index?typeId=' + this.data.index
        })
      }
    } else if (wx.getStorageSync('tk') != "123") {
      this.setData({
        shou: false
      })
    }
    else if (wx.getStorageSync('phone') != "123") {
      this.setData({
        shous: false
      })
    }
    
    // const status = await app.getAuth()
    // if (!status) return
    // let that = this
    // let data = {
    //   typeId: that.data.index,
    //   data: e.detail.value
    // }
    // wx.navigateTo({
    //   url: '/pages/filterLists/index?search=' + e.detail.value,
    // })
    // this.setData({
    //   value:''
    // })
  },
  goRouter(e) {
    console.log(e)
    if (wx.getStorageSync('tk') == "123" && wx.getStorageSync('phone') == "123"){
      wx.navigateTo({
        url: '/pages/user/index'
      })
    } else if (wx.getStorageSync('tk') != "123") {
      this.setData({
        shou: false
      })
    }
    else if (wx.getStorageSync('phone') != "123") {
      this.setData({
        shous: false
      })
    }
    
  },
  onShow: function () {
    wx.login({
      success: function (res) {
        console.log(res)
        app.request({
    url: '/login/login?code=' + res.code+"&avatar="+wx.getStorageSync('avatarurl')+'&nickname='+wx.getStorageSync('name'),
    success: function (res) {
      console.log(res)
      wx.setStorageSync('token',res.data.token)
      // wx.setStorageSync('session', res.data.session_key)
      // wx.setStorageSync('oprnid', res.data.openid)
    }
  });
      }
    })
     if(wx.getStorageSync('uid') == ""){
       console.log("kdjdjh")
  
}
    // console.log(wx.getStorageSync("usercodes"), wx.getStorageSync("name"), wx.getStorageSync("avatarurl"))
    // app.request({
    //   url: '/login/user?openid=' + wx.getStorageSync("usercodes") + '&nickname=' + wx.getStorageSync("name") + '&avatar=' + wx.getStorageSync("avatarurl"),
    //   success: function (res) {

    //     console.log(JSON.stringify(res.data.uid) + "获取uid")

    //     wx.setStorageSync('uid', res.data.uid)
    //   }
    // });
  },
  // getUserInfo: function(e) {
  //   console.log(e)
  //   app.globalData.userInfo = e.detail.userInfo
  //   this.setData({
  //     userInfo: e.detail.userInfo,
  //     hasUserInfo: true
  //   })
  // }
   onShareAppMessage: function () {
    path: '/pages/index/index'
  }
})